package com.事件处理event.Google恐龙;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import java.util.Vector;

public class MyPanel extends JPanel implements Runnable, KeyListener {
    MyImage i1 = null;
    MyImage i2 = null;
    Circle c = null;
    boolean isLive = true;
    Barrier b1 = null;
    Vector<Barrier> vector = new Vector<>();


    public MyPanel() {

        i1 = new MyImage(0, 100);
        i2 = new MyImage(1605, 100);
        i1.image = i2;
        i2.image = i1;
        i1.start();
        i2.start();
        new Thread(this).start();
        c = new Circle(50, 623 - 15, 50, this);
        b1 = new Barrier(1200, 623 - 15);
        b1.start();
        vector.add(b1);
        Barrier.vector = vector;
    }

    @Override
    public void paint(Graphics g) {


        super.paint(g);
        Font s = new Font("宋体", Font.PLAIN, 50);
        g.setFont(s);
        g.setColor(Color.black);
        g.drawString(Recorder.num + "     " + Recorder.Step_count / 100, 100, 50);

        g.setColor(Color.black);
        g.drawImage(i1.i, i1.x, i1.y, 1605, 623, this);
        g.drawImage(i2.i, i2.x, i2.y, 1605, 623, this);

        g.fillOval(c.x, c.y, c.r, c.r);
        for (int i = 0; i < vector.size(); i++) {
            Barrier b = vector.get(i);
            Touch(b);
            if (b.x < 0) {
                vector.remove(i);
            }

            g.fillRect(b.x, b.y, 50, 100);
        }


    }

    public void Touch(Barrier b) {
        int c1 = c.x;
        int b1 = b.x;
        if (b.x <= c.x && b.x >= c.x - 2 * c.r) {
            if (c.y + 2 * c.r >= b.y) {
                System.out.println("碰到了");
                b.isLive  = false ;
                MyImage.isLive = false;
                isLive = false;
            }
        }

    }

    @Override
    public void run() {
        while (true && isLive) {
            repaint();
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_SPACE && c.pool) {
            new Thread(c).start();

        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}